#include "MainWindow.h"
#include "SphereDialog.h"
#include "RandomDialog.h"
#include <wx/dirdlg.h>
#include <string>

using std::string;


MainWindow::MainWindow( wxWindow* parent )
:
MainWindow_Base( parent )
{
}

void MainWindow::onThresholdScroll( wxScrollEvent& event )
{
	if(selfGen_checkBox->IsChecked())
	{
		long xRes, yRes, zRes;
		xRes_textCtrl->GetValue().ToLong(&xRes);
		yRes_textCtrl->GetValue().ToLong(&yRes);
		zRes_textCtrl->GetValue().ToLong(&zRes);

		float threshold = getThreshold();

		glCanvas->setMesh(mc.generateMesh(xRes, yRes, zRes, threshold));
		float r, g, b;
		thresholdToColor(threshold, &r, &g, &b);
		glCanvas->setColor(r, g, b);
		Refresh();
	}
}

void MainWindow::onGenerateButton( wxCommandEvent& event )
{
	long xRes, yRes, zRes;
	xRes_textCtrl->GetValue().ToLong(&xRes);
	yRes_textCtrl->GetValue().ToLong(&yRes);
	zRes_textCtrl->GetValue().ToLong(&zRes);

	float threshold = getThreshold();

	glCanvas->setMesh(mc.generateMesh(xRes, yRes, zRes, threshold));
	float r, g, b;
	thresholdToColor(threshold, &r, &g, &b);
	glCanvas->setColor(r, g, b);
	Refresh();
}

void MainWindow::changeDrawMode( wxCommandEvent& event )
{
	// TODO: Implement changeDrawMode
}

void MainWindow::onShowGrid( wxCommandEvent& event )
{
	glCanvas->showGrid(showGrid_checkBox->IsChecked());
	glCanvas->Refresh();
}

void MainWindow::onNewMenu( wxCommandEvent& event )
{
	control_panel->Enable(false);
	glCanvas->setMesh(0);
	glCanvas->Refresh();
}

void MainWindow::onImportMenu( wxCommandEvent& event )
{
	//const wxChar *types = _("Dir (*.dae)|*.dae|");
	//const wxChar *types = _("");

	wxDirDialog dialog(this, _("Select CT Data"));

	if (dialog.ShowModal() == wxID_OK)
	{
		statusBar->SetStatusText(_("Carregando a dados..."), 0);
		mc.setSpatialData(new CTData(string(dialog.GetPath().ToAscii())));
		control_panel->Enable(true);
		statusBar->SetStatusText(_("Pronto."), 0);
		Refresh();
	}
}

void MainWindow::addPrimitive( wxCommandEvent& event )
{
	//Sphere
	if(event.GetId() == (primitives_menu->FindItemByPosition(0))->GetId())
	{
	    SphereDialog dialog(this);

	    if (dialog.ShowModal() == wxID_OK)
	    {
	    	mc.setSpatialData(dialog.getSphere());
	    	control_panel->Enable(true);
	    }
	}
	//RandomData
	else if(event.GetId() == (primitives_menu->FindItemByPosition(1))->GetId())
	{
	    RandomDialog dialog(this);

	    if (dialog.ShowModal() == wxID_OK)
	    {
	    	mc.setSpatialData(dialog.getRandomData());
	    	control_panel->Enable(true);
	    }
	}

	Refresh();
}

void MainWindow::onAboutMenu( wxCommandEvent& event )
{
    wxString msg;
    msg.Printf(wxT("Trabalho:\n\t- Marching Cubes\n\nDisciplina:\n\t- CGII\n\nAutores:\n\t- Caio Melo\n\t- Jonas Deyson\n\nProfessor:\n\t- Joaquim Bento"));
    wxMessageBox(msg, wxT("About"), wxOK | wxICON_INFORMATION, this);
}


float MainWindow::getThreshold()
{
	float min, max;
	mc.getSpatialData()->getValueRange(&min, &max);

	float a = (max - min)/((float)(threshold_slider->GetMax() - threshold_slider->GetMin()));

	return a*(threshold_slider->GetValue() + fabs(threshold_slider->GetMin())) + min;
}

void MainWindow::thresholdToColor(float thrashold, float *r, float *g, float *b)
{
	float min, max;
	mc.getSpatialData()->getValueRange(&min, &max);

	float k = (2.0/max)*thrashold;

	if(k <= 1.0)
	{
		*b = 1.0-k;
		*g = k;
		*r = 0.0;
		return;
	}
	if((k > 1.0) && (k <= 2.0))
	{
		*b = 0.0;
		*g = 2.0 - k;
		*r = k - 1.0;
		return;
	}
}
